package com.cabal.common.util;

public class HighLowSwapUtil {

    /**
     * 字节流排序低字节在前高字节在后
     * @param count
     * @return
     */
    public static String lowHigh(Integer count){
        // 分别取出int的高字节跟低字节
        int big = (count & 0xFF00) >> 8;
        int little = count & 0xFF;
        String str_big = Integer.toHexString(big);
        if (str_big.length() < 2){
            str_big = "0"+str_big;
        }
        String str_little = Integer.toHexString(little);
        if(str_little.length() < 2){
            str_little = "0"+str_little;
        }
        String xString =  str_little+str_big;
        return xString.toUpperCase();  //转大写
    }

    /**
     * 字节流排序高字节在前低字节在后
     * @param count
     * @return
     */
    public static String highLow(Integer count){
        // 分别取出int的高字节跟低字节
        int big = (count & 0xFF00) >> 8;
        int little = count & 0xFF;
        String str_big = Integer.toHexString(big);
        if (str_big.length()<2){
            str_big = "0"+str_big;
        }
        String str_little = Integer.toHexString(little);
        if(str_little.length()<2){
            str_little = "0"+str_little;
        }
        String xString =  str_big+str_little;
        return xString.toUpperCase();  //转大写
    }

    public static String byteToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                // 如果是一位的话，要补0
                hexString.append('0');
            }
            hexString.append(hex.toUpperCase());
        }
        return hexString.toString();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }

}
